package com.acme.ioniolab.copyfile.implementation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.acme.ioniolab.copyfile.FileCopier;

public class NIOFileCopier implements FileCopier {

	public void copyFile(String srcFileName, String destFileName) throws IOException {
		File src = new File(srcFileName);
		File dest = new File(destFileName);
		
		if (!src.exists()) {
			throw new IOException("File "+srcFileName+" not found");
		}
		if (!dest.exists()) {
			if (!dest.createNewFile()) {
				throw new IOException("File "+destFileName+" doesn't exist and couldn't be created");
			}
		}
		
		try (FileChannel inputChannel = new FileInputStream(src).getChannel();
				FileChannel outputChannel = new FileOutputStream(dest).getChannel()) {
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		}
	}
}
